/*
 * 
 */

package com.memorytech.starchive;

import jp.co.memory_tech.starchive.R;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.memorytech.starchive.chart.PieChartView;
import com.memorytech.starchive.entity.HistoryBarcode;

/**
 * The Class HistoryProductDetail.
 * 
 * @author Tam-LT
 */
public class HistoryProductDetail extends Fragment {

    /** The Constant ARG_POSITION. */
    final static String HISTORY_BARCODE = "position";

    /** The history barcode. */
    private HistoryBarcode historyBarcode;

    /**
     * @return the historyBarcode
     */
    public HistoryBarcode getHistoryBarcode() {
        return historyBarcode;
    }

    /**
     * @param historyBarcode the historyBarcode to set
     */
    public void setHistoryBarcode(HistoryBarcode historyBarcode) {
        this.historyBarcode = historyBarcode;
    }

    /** The view. */
    private View view;

    /*
     * (non-Javadoc)
     * @see
     * android.support.v4.app.Fragment#onCreateView(android.view.LayoutInflater,
     * android.view.ViewGroup, android.os.Bundle)
     */
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        // If activity recreated (such as from screen rotate), restore
        // the previous HistoryBarcode selection set by
        // onSaveInstanceState().
        // This is primarily necessary when in the two-pane layout.
        if (savedInstanceState != null) {
            historyBarcode = (HistoryBarcode) savedInstanceState.get(HISTORY_BARCODE);
        }
        view = inflater.inflate(R.layout.history_product_detail, container, false);
        // Inflate the layout for this fragment
        return view;
    }

    /**
     * Calculate data.
     * 
     * @param data the data
     * @return the float[]
     */
    private float[] calculateData(float[] data) {
        // TODO Auto-generated method stub
        float total = 0;
        for (int i = 0; i < data.length; i++)
        {
            total += data[i];
        }
        for (int i = 0; i < data.length; i++)
        {
            data[i] = 360 * (data[i] / total);
        }
        return data;

    }

    /*
     * (non-Javadoc)
     * @see android.support.v4.app.Fragment#onStart()
     */
    @Override
    public void onStart() {
        super.onStart();

        // During startup, check if there are arguments passed to the fragment.
        // onStart is a good place to do this because the layout has already
        // been
        // applied to the fragment at this point so we can safely call the
        // method
        // below that sets the article text.
        Bundle args = getArguments();
        if (args != null) {
            // Set article based on argument passed in
            updateProductDetail((HistoryBarcode) args.get(HISTORY_BARCODE));
        } else if (historyBarcode != null) {
            // Set article based on saved instance state defined during
            // onCreateView
            updateProductDetail(historyBarcode);
        }
    }

    /**
     * Update product detail.
     * 
     * @param position the position
     */
    public void updateProductDetail(HistoryBarcode historyBarcode) {

        if (historyBarcode != null) {
            this.historyBarcode = historyBarcode;

            ((TextView) getActivity().findViewById(R.id.freeMemory)).setText(getActivity()
                    .getString(
                            R.string.free_memory)
                    + ": " + historyBarcode.getFreeSpace());
            ((TextView) getActivity().findViewById(R.id.usedMemory)).setText(getActivity()
                    .getString(
                            R.string.used_memory)
                    + ": " + historyBarcode.getUsedSpace());

            ViewGroup viewGroup = (ViewGroup) view.findViewById(R.id.pie_chart);
            viewGroup.removeAllViews();
            float values[] = new float[] {
                    historyBarcode.getFreeSpace(), historyBarcode.getUsedSpace()
            };
            int[] COLORS = new int[] {
                    Color.parseColor("#FC0AE8"), Color.BLUE
            };

            values = calculateData(values);
            PieChartView pieChartView = new PieChartView(getActivity(), values, COLORS);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                    LayoutParams.FILL_PARENT,
                    LayoutParams.WRAP_CONTENT);
            params.gravity = Gravity.CENTER;
            params.leftMargin = 200;
            pieChartView.setLayoutParams(params);
            viewGroup.addView(pieChartView, params);
            viewGroup.invalidate();
        } else {
            ((TextView) getActivity().findViewById(R.id.freeMemory)).setText("");

            ((TextView) getActivity().findViewById(R.id.usedMemory)).setText("");
            ViewGroup viewGroup = (ViewGroup) view.findViewById(R.id.pie_chart);
            viewGroup.removeAllViews();
        }
    }

    /*
     * (non-Javadoc)
     * @see
     * android.support.v4.app.Fragment#onSaveInstanceState(android.os.Bundle)
     */
    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // Save the current HISTORY_BARCODE selection in case we need to
        // recreate the
        // fragment
        outState.putSerializable(HISTORY_BARCODE, historyBarcode);
    }

    /*
     * (non-Javadoc)
     * @see android.support.v4.app.Fragment#onStop()
     */
    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        if (historyBarcode != null) {
            updateProductDetail(historyBarcode);
        }
    }

    /**
     * On key back.
     */
    public void onKeyBack() {
        // TODO Auto-generated method stub
        onStop();

    }

}
